package com.lf.cloud.grab.business;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.lf.cloud.comm.dao.GrabAimeishishangInfDao;
import com.lf.cloud.comm.dao.GrabUploadInfDao;
import com.lf.cloud.comm.modal.pojo.GrabAimeishishangInf;
import com.lf.cloud.comm.modal.pojo.GrabUploadInf;
import com.lf.cloud.comm.webutils.DateUtils;
import com.lf.cloud.comm.webutils.GetWaterId;
import com.lf.cloud.grab.utils.CommMethod;
import com.lf.cloud.grab.utils.FtpUtils;
import com.lf.cloud.grab.utils.HttpPostUtils;

public class GrabBusiness {

	//public  String LOCALPATH = "D://Manipula/Manipula/pic/aimeishishang/";

	
	
	FTPClient ftpClient = new FTPClient();
	
	
	
	
	  
	/**  
	* @Title: getImgUpload  
	* @Description: TODO(这里用一句话描述这个方法的作用)  
	* @param url
	* @param pageNum 页数
	* @param divNum  第几页中的第几个div
	* @param imgNum	 第几页中的第几个div中的第几张图
	* @param uploadType
	* @param localPath
	* @throws Exception
	* @return void
	* @author lxliufei
	* @date 下午5:41:00
	* @throws  
	*/  
	public synchronized  void getImgUpload(String url ,String pageNum ,String divNum , String imgNum , String uploadType ,String localPath) throws Exception{
		
		if("1".equals(uploadType)){
			ftpClient = FtpUtils.loginFTP();
		}
		String host = "http://www.aimeishishang.com/";
		Document document = CommMethod.getDocument(url , pageNum);
		System.out.println("====>pagePage url: "+url);
		List<String> documentPageNum = CommMethod.getDocumentPageNum(document , pageNum);
		System.out.println("===> 页数："+documentPageNum.size());
		
		if(documentPageNum!=null && documentPageNum.size()>0){
			for (int i = 0; i < 6; i++) {
				String urlStr = documentPageNum.get(i);
				
				System.out.println("********************************************");
				System.out.println("====> currPageUrl : "+urlStr);
				System.out.println("*******************************************");
				getCurrPageImg(divNum, imgNum, uploadType, localPath, host, urlStr);
			}
		}else{
			getCurrPageImg(divNum, imgNum, uploadType, localPath, host, url);
		}
		
		if("1".equals(uploadType)){
			ftpClient = FtpUtils.loginOutFTP(ftpClient);
		}
	}



	public void getCurrPageImg(String divNum, String imgNum, String uploadType,
			String localPath, String host, String urlStr) throws IOException {
		Document doc = CommMethod.getDocument(urlStr);
		if("1".equals(uploadType)){
			getCurrPageToFTP(divNum, imgNum, host, doc , localPath);
		}else{
			getCurrPageToLocal(divNum, imgNum, host, doc , localPath);
		}
	}

	
	
	/**
	 * 	
	 *  获取一个页面的的数据并且上传
	 * 
	 * @param divNum
	 * @param imgNum
	 * @param host
	 * @param document
	 * @throws IOException
	 */
	private  void getCurrPageToFTP(String divNum, String imgNum, String host,
			Document document , String localPath) throws IOException {
		List<String> divHrefList = CommMethod.getDivHrefList(document);
		if(divHrefList==null || divHrefList.size()==0){
			System.out.println("getDivHref-list  is null ! ");
			return;
		}
		for (int i = 0 ; i<divHrefList.size() ;  i ++) {
			String divLink = null;
			if(StringUtils.isNotEmpty(divNum)){
				i = Integer.parseInt(divNum)-1;
				divNum = "";
			}
			divLink = divHrefList.get(i);
			
			String[] split = divLink.split("/");
			String imgdir = split[split.length-2];
			String imgdir2 =split[split.length-1];
			boolean changeWorkingDirectory = ftpClient.changeWorkingDirectory(imgdir);
			System.out.println("===> 切换文件目录："+imgdir);
			
			if(!changeWorkingDirectory){
				System.out.println("===> 文件目录不存在， 创建......：");
				boolean makeDirectory = ftpClient.makeDirectory(imgdir);
				System.out.println("===> 文件目录不存在， 创建......："+makeDirectory);
				
				ftpClient.changeWorkingDirectory(imgdir);
			}

            
			List<String> divContent = getDivContent(divLink);
			String logStr = "获取第"+(i+1)+"个div 内容";
			System.out.println("====> "+logStr+",size:"+divContent.size());
			if(divContent!=null && divContent.size() >0 ){
				boolean imgdir2_d = ftpClient.changeWorkingDirectory(imgdir2);
				System.out.println("===> 切换文件目录："+imgdir2);
				if(!imgdir2_d){
					System.out.println("===> 文件目录不存在， 创建......：");
					boolean makeDirectory = ftpClient.makeDirectory(imgdir2);
					System.out.println("===> 文件目录不存在， 创建......："+makeDirectory);
					ftpClient.changeWorkingDirectory(imgdir2);
				}
				System.out.println(" file path 1 :"+ ftpClient.printWorkingDirectory());
				for (int j = 0 ; j<divContent.size() ;  j ++) {
					if(StringUtils.isNotEmpty(imgNum)){
						j = Integer.parseInt(imgNum)-1;
						imgNum = "";
					}
					
					String logForimg = "第"+(j+1)+"张图片";
					String string = divContent.get(j);
					try {
						String imgURL = "";
						if(string.contains("http")){
							imgURL = string;
						}else{
							imgURL = host+string;
						}
						byte[] imgByte = HttpPostUtils.getImgByte(imgURL);
						ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(imgByte);
						System.out.println("====>"+logStr+", 上传"+logForimg+"......");
						FtpUtils.UploadToFtp(null, ftpClient, byteArrayInputStream);
						String uploadPath = ftpClient.printWorkingDirectory();
						this.insertGrabUploadInf(uploadPath, localPath+"/"+imgdir);
					} catch (Exception e) {
						System.out.println("====>"+logStr+", 获取"+logForimg+"失败......");
					}
				}
				ftpClient.changeToParentDirectory();
			}
			/*if(i>=2){
				break;
			}*/
			ftpClient.changeToParentDirectory();
		}
	}
	private  void getCurrPageToLocal(String divNum, String imgNum, String host,
			Document document , String localPath) throws IOException {
		List<String> divHrefList = CommMethod.getDivHrefList(document);
		if(divHrefList==null || divHrefList.size()==0){
			System.out.println("getDivHref-list  is null ! ");
			return;
		}
		for (int i = 0 ; i<divHrefList.size() ;  i ++) {
			String divLink = null;
			if(StringUtils.isNotEmpty(divNum)){
				i = Integer.parseInt(divNum)-1;
				divNum = "";
			}
			divLink = divHrefList.get(i);
			
			String[] split = divLink.split("/");
			String imgdir = split[split.length-2];
			String imgdir2 =split[split.length-1];
			
			
			List<String> divContent = getDivContent(divLink);
			String logStr = "获取第"+(i+1)+"个div 内容";
			System.out.println("====> "+logStr+",size:"+divContent.size());
			if(divContent!=null && divContent.size() >0 ){

				for (int j = 0 ; j<divContent.size() ;  j ++) {
					if(StringUtils.isNotEmpty(imgNum)){
						j = Integer.parseInt(imgNum)-1;
						imgNum = "";
					}
					String logForimg = "第"+(j+1)+"张图片";
					
					String addStr = divContent.get(j);
					
					String[] split2 = addStr.split("@@@");
					
					String string = split2[0];;
					try {
						String imgURL = "";
						if(string.contains("http")){
							imgURL = string;
						}else{
							imgURL = host+string;
						}
						byte[] imgByte = HttpPostUtils.getImgByte(imgURL);
						System.out.println("====>"+logStr+", 上传"+logForimg+"......");
						String fileName = FtpUtils.DownloadToLocal(localPath, imgdir, imgByte);
						String fileDirPath = localPath+"/"+imgdir;
						if(localPath.indexOf("aimeishishang")>0){
							if(split2.length>=2){
								this.insertGrabAimeishishangInf(fileName, fileDirPath, split2[1]);
							}
						}else{
							this.insertGrabUploadInf(fileName, fileDirPath);
						}
					} catch (Exception e) {
						System.out.println("====>"+logStr+", 获取"+logForimg+"失败......");
					}
				}
			}
		}
	}
	
	
	
	
	/**
	 * 
	 * 获取所有ImgUrl
	 * 
	 * @param url
	 * @return
	 */
	public static List<String> getDivContent(String url){
		System.out.println("====>getDivContent  request url : "+url);
		
		Document document = CommMethod.getDocument(url);
		
		Elements elementsByVal = document.getElementsByAttributeValue("id", "thread_subject");
		String tit_id = "";
		String tit_txt = "";
		if(elementsByVal.size()>0){
			tit_txt = elementsByVal.get(0).text();
			String[] split = url.split("/");
			if(split.length>0){
				tit_id = split[split.length-1];
			}
			
		}
		
		System.out.println(tit_id + "@@@"+tit_txt);
		String resStrTit = tit_id + "###"+tit_txt;
		Elements elementsByTag = document.getElementsByTag("ignore_js_op");
		List<String> imgUrlList = new ArrayList<String>();

		if(elementsByTag != null && elementsByTag.size() > 0 ){
			for (Element element : elementsByTag) {
				String resStr = "";
				String imgUrl = element.childNode(1).attr("src");
				resStr += imgUrl+"@@@"+resStrTit;
				imgUrlList.add(resStr);
			}
		}
		return imgUrlList;
	}
	
	
	
	
	  
	/**  
	* @Title: insertGrabUploadInf  
	* @Description: 添加上传信息表
	* @param @param fileName
	* @param @param uploadPath
	* @return void
	* @author lxliufei
	* @date 下午4:27:35
	* @throws  
	*/  
	    
	public void insertGrabUploadInf(String fileName , String uploadPath ){
		
		GrabUploadInf gur = new GrabUploadInf();
		
		gur.setSerialId("_upload"+GetWaterId.getWaterId());
		
		gur.setFileName(fileName);
		
		gur.setFileShowName(fileName);
		
		//String[] split = fileName.split(",");
		
		gur.setExpandName(fileName.split("\\.")[1]);
		
		String substring = uploadPath.substring(uploadPath.indexOf("grab"), uploadPath.length());
		substring = substring.replace("\\\\", "/");
		substring = substring.replace("\\", "/");
		substring = substring.replace("////", "/");
		substring = substring.replace("//", "/");
		
		gur.setUploadPath(substring);
		String[] split = substring.split("/");
		gur.setFileType(split[split.length-1]);
		
		gur.setUploadTime(DateUtils.getCurrDateTime());
		
		gur.setUploadUser(null);
		
		GrabUploadInfDao.me.insert(gur);
		
	}
	/**  
	 * @Title: insertGrabUploadInf  
	 * @Description: 添加上传信息表
	 * @param @param fileName
	 * @param @param uploadPath
	 * @return void
	 * @author lxliufei
	 * @date 下午4:27:35
	 * @throws  
	 */  
	
	public void insertGrabAimeishishangInf(String fileName , String uploadPath ,String titStr){
		
		GrabAimeishishangInf gur = new GrabAimeishishangInf();
		
		gur.setSerialId("_upload"+GetWaterId.getWaterId());
		
		gur.setFileName(fileName);
		
		gur.setFileShowName(fileName);
		
		//String[] split = fileName.split(",");
		if(StringUtils.isNotEmpty(titStr)){
			
			String[] split = titStr.split("###");
			if(split.length>0){
				gur.setTitleId(split[0]);
				gur.setTitleName(split[1]);
				System.out.println(split[0]+"---------"+split[1]);
			}
		}
		gur.setExpandName(fileName.split("\\.")[1]);
		String substring = uploadPath.substring(uploadPath.indexOf("grab"), uploadPath.length());
		substring = substring.replace("\\\\", "/");
		substring = substring.replace("\\", "/");
		substring = substring.replace("////", "/");
		substring = substring.replace("//", "/");
		
		gur.setUploadPath(substring);
		String[] split = substring.split("/");
		gur.setFileType(split[split.length-1]);
		
		gur.setUploadTime(DateUtils.getCurrDateTime());
		
		gur.setUploadUser(null);
		GrabAimeishishangInfDao.me.insert(gur);
		
	}
	
	
	public void insertGrabUploadInfToLocal(String fileName , String uploadPath ){
		//String[] split = fileName.split(",");
		GrabUploadInf gur = getGrabUploadInfObj(uploadPath, fileName);
		GrabUploadInfDao.me.insert(gur);
		
	}
	public void insertGrabUploadInfToLocal(Map<String , String> map){
		List<GrabUploadInf> objlist =new  ArrayList<GrabUploadInf>();
		for (Map.Entry<String , String> entry : map.entrySet()) {
			//截取文件路径
			String filePath = entry.getKey().split("@")[0];
			String fileName = entry.getValue();
			//获取数据对象
			GrabUploadInf gur = getGrabUploadInfObj(filePath, fileName);
			objlist.add(gur);
		}
		System.out.println("====>SIZE:"+objlist.size());
		GrabUploadInfDao.me.batchAdd(objlist , 50);
		
		
	}



	  
	/**  
	* @Title: getGrabUploadInfObj  
	* @Description: 获取 GrabUploadInf 对象
	* @param @param filePath
	* @param @param fileName
	* @param @return
	* @return GrabUploadInf
	* @author lxliufei
	* @date 下午5:57:04
	* @throws  
	*/  
	private GrabUploadInf getGrabUploadInfObj(String filePath, String fileName) {
		GrabUploadInf gur = new GrabUploadInf();
		gur.setSerialId("_upload"+GetWaterId.getWaterId());
		gur.setFileName(fileName);
		gur.setFileShowName(fileName);
		//String[] split = fileName.split(",");
		gur.setExpandName(fileName.split("\\.")[1]);
		String substring = filePath;
		substring = substring.replace("\\\\", "/");
		substring = substring.replace("\\", "/");
		substring = substring.replace("////", "/");
		substring = substring.replace("//", "/");
		substring = substring.substring(substring.indexOf("grab") , substring.length());
		gur.setUploadPath(substring);
		String[] split = substring.split("/");
		gur.setFileType(split[split.length-1]);
		gur.setUploadTime(DateUtils.getCurrDateTime());
		gur.setUploadUser(null);
		return gur;
	}
	
	
	
	
	
	
	
	
	
	
	
	
}
